package vip.websky.project.web.controller;

import com.alibaba.fastjson.JSON;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import vip.websky.project.common.config.SysConst;
import vip.websky.project.module.entity.user.SysUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 公共controller
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/8/28-19:35
 */
@Slf4j
@Controller
public class BaseController {

    /**
     * 1、获取session中对象名称
     *
     * @param req      request
     * @param attrName 参数名称
     * @return 参数值
     */
    protected Object getSessionObject(HttpServletRequest req, String attrName) {
        Object sessionObj = null;
        HttpSession session = req.getSession(false);
        if (session != null) {
            sessionObj = session.getAttribute(attrName);
        }
        return sessionObj;
    }

    /**
     * 2、设置session中对象值
     *
     * @param req       request
     * @param attrName  参数名称
     * @param attrValue 参数值
     */
    protected void setSessionObject(HttpServletRequest req, String attrName, Object attrValue) {
        HttpSession session = req.getSession();
        session.setAttribute(attrName, attrValue);
    }

    protected void cleanSession(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.invalidate();
    }

    /**
     * 3、发送页面消息
     *
     * @param request  request
     * @param response response
     * @param data     发送的json内容
     */
    protected void sendMessages(HttpServletRequest request, HttpServletResponse response, Object data) {
        response.setCharacterEncoding(SysConst.SYS_CHARSET);
        response.setHeader("Content-Type", "charset=UTF-8");
        if (request != null
                && request.getHeader("USER-AGENT") != null
                && StrUtil.containsIgnoreCase(request.getHeader("USER-AGENT"), "Firefox")) {
            response.setContentType("application/json;charset=UTF-8");
        }
        try {
            log.info("返回页面结果消息：【{}】", JSON.toJSONString(data));
            response.getWriter().print(JSON.toJSONString(data));
        } catch (Exception e) {
            log.info("sendMessages fail, Exception：{}", e);
        }finally {
            log.info("======================分==============割==============线=================================");
        }
    }

    /**
     * 4、获取用户User
     *
     * @param req
     * @return
     */
    protected SysUser getUser(HttpServletRequest req) {
        HttpSession session = req.getSession();//获取session
        return (SysUser) session.getAttribute(SysConst.USER_SESSION_KEY);
    }

    /**
     * 5、退出登录
     *
     * @param request
     */
    protected void loginOut(HttpServletRequest request) {
        //退出登录
        cleanSession(request);
    }

    /**
     * 6、登录
     *
     * @param request
     * @param loginUser 用户信息
     */
    protected void loginIn(HttpServletRequest request, SysUser loginUser) {
        setSessionObject(request, SysConst.USER_SESSION_KEY, loginUser);
    }

}
